package com.pc.controller;

import com.pc.enrty.ShoppingAddress;
import com.pc.enrty.User;
import com.pc.enrty.User;
import com.pc.pojo.Basic;
import com.pc.service.ShoppingAddressService;
import com.pc.service.UserService;
import com.pc.util.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.plaf.SliderUI;
import java.util.ArrayList;

@RestController
@RequestMapping("/pc/address/")
public class ShoppingAddressController {
    @Autowired
    private ShoppingAddressService shoppingAddressService;
    @Autowired
    private UserService userService;
    @Autowired
    private ResultObject result;
    @Autowired
    private User user;
    /**
     * 根据uid查找收货地址
     * @return
     */
    @RequestMapping("find")
    public ResultObject findById(ShoppingAddress shoppingAddress){
        ArrayList<ShoppingAddress> byUid = shoppingAddressService.findByUid(shoppingAddress);
        if(byUid.size()>0){
            result.setStatus(400);
            result.setResult(byUid);
        }
        else{
            result.setStatus(404);
            result.setResult("找不到地址,请添加地址");
        }
        return result;
    }

    /**
     * 添加收货地址,根据uid将sname修改到user表中的nickname
     * @return
     */
    @RequestMapping("add")
    public ResultObject addAddress(ShoppingAddress shoppingAddress){
        String sname = shoppingAddress.getSname();
        Integer uid = shoppingAddress.getUid();
        user.setId(uid);
        user.setNickname(sname);
        boolean b = userService.updateNickName(user);
        if(b){
            result.setStatus(400);
            result.setResult("添加到user表成功");
        }else{
            result.setStatus(405);
            result.setResult("添加到user表失败");
        }
        return result;
    }

    /**
     * 根据uid修改收货地址
     * @return
     */
    @RequestMapping("update")
    public ResultObject updateAddress(ShoppingAddress shoppingAddress){
        boolean b = shoppingAddressService.updateShoppingAddress(shoppingAddress);
        ArrayList<ShoppingAddress> list = shoppingAddressService.findByUid(shoppingAddress);
        if(b){
            result.setStatus(400);
            result.setResult(list);
        }else{
            result.setStatus(405);
            result.setResult("修改失败");
        }

        return result;
    }
    @RequestMapping("findById")
    public ResultObject findAdressById(ShoppingAddress shoppingAddress){
        ArrayList<ShoppingAddress> list = shoppingAddressService.findById(shoppingAddress);
        if(list.size()>0){
            result.setStatus(400);
            result.setResult(list);
        }
        else{
            result.setStatus(404);
            result.setResult("找不到地址,请添加地址");
        }
        return result;
    }
    @RequestMapping("addAddress")
    public ResultObject addAddressToAddress(ShoppingAddress shoppingAddress){
        boolean b = shoppingAddressService.addShoppingAddress(shoppingAddress);
        ArrayList<ShoppingAddress> list = shoppingAddressService.findByUid(shoppingAddress);
        if(b){
            result.setStatus(400);
            result.setResult(list);
        }else{
            result.setStatus(405);
            result.setResult("添加失败");
        }

        return result;
    }

    @RequestMapping("findbasic")
    public ArrayList<Basic> findbasic(User user) {
        return  userService.qureyPhoneById(user);
    }

}
